<template>
  <div class="app-container">
    <!--查询表单-->
    <el-form :inline="true" class="demo-form-inline" style="width: 2500px">
      <el-form-item label="时间">
        <el-date-picker
          v-model="searchObj.startTime"
          type="datetime"
          placeholder="选择起始时间"
        ></el-date-picker>
        <el-date-picker
          v-model="searchObj.endTime"
          type="datetime"
          placeholder="选择终止时间"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="充电站点">
        <el-select
          v-model="searchObj.addressSite"
          placeholder="请选择"
          clearable
        >
          <el-option
            label="广州天河体育东共享充电站"
            value="广州天河体育东共享充电站"
          />
          <el-option
            label="广州天河体育西共享充电站"
            value="广州天河体育西共享充电站"
          />
          <el-option
            label="广州天河体育南共享充电站"
            value="广州天河体育南共享充电站"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="处理状态">
        <el-select v-model="searchObj.status" placeholder="请选择" clearable>
          <el-option label="登记" value="0" />
          <el-option label="已派单" value="1" />
          <el-option label="未处理" value="2" />
          <el-option label="处理中" value="3" />
          <el-option label="已处理" value="4" />
          <el-option label="已完成" value="5" />
        </el-select>
      </el-form-item>

      <el-button
        type="primary"
        icon="el-icon-search"
        @click="fetchData()"
      >
        查询
      </el-button>
      <el-button type="default" style="margin-right: 5px;margin-left: 5px;" @click="resetData()">
        清空
      </el-button>
      <!-- 新增 -->
      <router-link
        :to="'/charging-pile/addhitch_manager'"
        style="margin-right: 5px"
      >
        <el-button type="primary" icon="el-icon-edit">
          新增
        </el-button>
      </router-link>
      <!-- 批量删除 -->
      <el-button @click="removeIds" type="danger">
        批量删除
      </el-button>
      <!-- Excel导出按钮 -->
      <el-button
        @click="exportData"
        type="primary"
        icon="el-icon-upload2"
      >
        导出Excel
      </el-button>
    </el-form>
    <!-- 列表 -->
    <el-table
      :data="list"
      border
      stripe
      @selection-change="handleSelectionChange"
    >
      <!-- 复选框 -->
      <el-table-column type="selection" />
      <el-table-column label="序号">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="addressSite" label="充电站点名称" />
      <el-table-column prop="chargingpileType" label="充电桩编号" />
      <el-table-column prop="address" label="地址位置" />
      <el-table-column prop="hitchType" label="故障类型">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.hitchType === 0" type="danger" size="mini">
            通讯故障
          </el-tag>
          <el-tag
            v-else-if="scope.row.hitchType === 1"
            type="danger"
            size="mini"
          >
            网关故障
          </el-tag>
          <el-tag
            v-else-if="scope.row.hitchType === 2"
            type="danger"
            size="mini"
          >
            配件故障
          </el-tag>
          <el-tag
            v-else-if="scope.row.hitchType === 3"
            type="danger"
            size="mini"
          >
            主板故障
          </el-tag>
          <el-tag
            v-else-if="scope.row.hitchType === 4"
            type="danger"
            size="mini"
          >
            过流
          </el-tag>
          <el-tag
            v-else-if="scope.row.hitchType === 5"
            type="danger"
            size="mini"
          >
            过压
          </el-tag>
          <el-tag
            v-else-if="scope.row.hitchType === 6"
            type="danger"
            size="mini"
          >
            电器短路
          </el-tag>
          <el-tag
            v-else-if="scope.row.hitchType === 7"
            type="danger"
            size="mini"
          >
            线路老化
          </el-tag>
          <el-tag v-else type="danger" size="mini">类型无法确定</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="content" label="故障内容" />
      <el-table-column prop="regTime" label="发生时间" />
      <el-table-column prop="person" label="处理人">
        <!-- <template slot-scope="scope">
          <span v-if="scope.row.status != 1">
            {{ cpSolvePerson.personName }}
          </span>
        </template> -->
      </el-table-column>

      <el-table-column prop="status" label="处理状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="seuccse" size="mini">
            登记
          </el-tag>
          <el-tag v-if="scope.row.status === 1" type="seuccse" size="mini">
            已派单
          </el-tag>
          <el-tag v-if="scope.row.status === 2" type="seuccse" size="mini">
            未处理
          </el-tag>
          <el-tag v-else-if="scope.row.status === 3" type="seuccse" size="mini">
            处理中
          </el-tag>
          <el-tag v-else-if="scope.row.status === 4" type="seuccse" size="mini">
            已处理
          </el-tag>
          <el-tag v-else-if="scope.row.status === 5" type="seuccse" size="mini">
            已完成
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="330px" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini">
            <router-link
              :to="
                '/charging-pile/hitch_manager_detail/' +
                scope.row.id +
                '/' +
                scope.row.status
              "
            >
              查看
            </router-link>
          </el-button>
          <el-button v-if="scope.row.status === 0" type="primary" size="mini">
            <router-link
              :to="'/charging-pile/hitch_manager_compile/' + scope.row.id"
            >
              编辑
            </router-link>
          </el-button>
          <el-button v-if="scope.row.status === 0" type="primary" size="mini">
            <router-link
              :to="'/charging-pile/hitch_manager_flat/' + scope.row.id"
            >
              派发
            </router-link>
          </el-button>
          <el-button
            v-if="scope.row.status === 0"
            type="danger"
            size="mini"
            @click="removeById(scope.row.id)"
          >
            删除
          </el-button>
          <el-button
            v-if="scope.row.status === 1"
            type="primary"
            size="mini"
            @click="cpHitchManagerStatusTwo(scope.row.id)"
          >
            接收
          </el-button>
          <el-button v-if="scope.row.status === 2" type="primary" size="mini">
            <router-link
              :to="'/charging-pile/hitch_manager_transact/' + scope.row.id"
            >
              办理
            </router-link>
          </el-button>
          <el-button
            v-if="scope.row.status === 3"
            type="primary"
            size="mini"
            @click="cpHitchManagerStatusFourSubmit(scope.row.id)"
          >
            确认办理
          </el-button>
          <el-button
            v-if="scope.row.status === 4"
            type="primary"
            size="mini"
            @click="cpHitchManagerRegisterFirst(scope.row.id)"
          >
            重新办理
          </el-button>
          <el-button
            v-if="scope.row.status === 4"
            type="primary"
            size="mini"
            @click="cpHitchManagerStatusFive(scope.row.id)"
          >
            确认完成
          </el-button>
          <el-button
            v-if="scope.row.status != 0 && scope.row.status != 5"
            type="danger"
            size="mini"
          >
            <router-link
              :to="'/charging-pile/hitch_manager_return/' + scope.row.id"
            >
              退回
            </router-link>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[3, 5, 10]"
      style="padding: 30px 0"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="changePageSize"
      @current-change="changeCurrentPage"
    />
  </div>
</template>
<script>
import listApi from '@/api/charging-pile/hitch_manage'
export default {
  data() {
    return {
      list: null, // 数据列表
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 5, // 每页记录数
      searchObj: {}, // 查询条件
      loginRecordList: [], //会员登录日志
      dialogTableVisible: false, //对话框是否显示
      BASE_API: process.env.VUE_APP_BASE_API, //这里从.env.development获取
      multipleSelection: [], // 批量选择中选择的记录列表
      id: '',
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    changePageSize(pagesize) {
      this.limit = pagesize
      this.fetchData()
    },
    changeCurrentPage(pages) {
      this.page = pages
      this.fetchData()
    },
    fetchData() {
      listApi.findAll(this.page, this.limit, this.searchObj).then((res) => {
        console.log(res)
        this.list = res.data.list
        this.total = res.count
      })
    },
    resetData() {
      // 还原表单
      this.searchObj = {}
      // 重新查询
      this.fetchData()
    },
    //导出Excel并下载
    exportData() {
      console.log(this.BASE_API)
      window.location.href = this.BASE_API + '/admin/pitfall/cpHitch/export'
    },
    // 定义方法，进行请求接口调用
    // 当表格复选框选项发生变化的时候触发
    handleSelectionChange(selection) {
      console.log(selection)
      this.multipleSelection = selection
    },
    //单删
    removeById(id) {
      this.$confirm('此操作将永久删除该记录,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return listApi.removeById(id)
        })
        .then((res) => {
          this.$message({
            message: res.message,
            type: 'success',
          })
          this.fetchData() //刷新页面
        })
        .catch((error) => {
          //console.log('error', error)
          if (error === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消删除',
            })
          }
        })
    },

    //批量删除
    removeIds() {
      console.log('进来了')
      this.$confirm('此操作将永久删除故障信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // 确定执行 then 方法
          var idList = []
          // 遍历数组得到每个 id 值，设置到 idList 里面
          for (var i = 0; i < this.multipleSelection.length; i++) {
            var obj = this.multipleSelection[i]
            var id = obj.id
            console.log('id:' + id)
            idList.push(id)
          }
          if (this.multipleSelection.length < 1) {
            alert('请选择要删除的对象')
          } else if (this.multipleSelection.length > 0) {
            // 调用接口
            listApi.removeIds(idList).then((response) => {
              console.log('response:' + response)
              // 提示
              this.$message({
                type: 'success',
                message: '删除成功!',
              })
              // 刷新页面
              this.fetchData()
            })
          }
        })
        .catch((error) => {
          //console.log('error', error)
          if (error === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消删除',
            })
          }
        })
    },
    //接收
    cpHitchManagerStatusTwo(id) {
      this.$confirm('请问您是否接收, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.id = id
          console.log(this.id)
          return listApi.cpHitchManagerStatusTwo(id)
        })
        .then((res) => {
          this.$message({
            message: res.message,
            type: 'success',
          })
          this.fetchData() //刷新页面
        })
        .catch((error) => {
          //console.log('error', error)
          if (error === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消接收',
            })
          }
        })
    },
    //确认办理
    cpHitchManagerStatusFourSubmit(id) {
      this.$confirm('请问您是否确认办理, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.id = id
          console.log(this.id)
          return listApi.cpHitchManagerStatusFourSubmit(id)
        })
        .then((res) => {
          this.$message({
            message: res.message,
            type: 'success',
          })
          this.fetchData() //刷新页面
        })
        .catch((error) => {
          //console.log('error', error)
          if (error === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消确认办理',
            })
          }
        })
    },
    cpHitchManagerRegisterFirst(id) {
      this.$confirm('请问您是否重新办理, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.id = id
          console.log(this.id)
          return listApi.cpHitchManagerRegisterFirst(id)
        })
        .then((res) => {
          this.$message({
            message: res.message,
            type: 'success',
          })
          this.fetchData() //刷新页面
        })
        .catch((error) => {
          //console.log('error', error)
          if (error === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消重新办理',
            })
          }
        })
    },
    cpHitchManagerStatusFive(id) {
      this.$confirm('请问您是否继续操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return listApi.cpHitchManagerStatusFive(id)
        })
        .then((res) => {
          this.$message({
            message: res.message,
            type: 'success',
          })
          this.fetchData() //刷新页面
        })
        .catch((error) => {
          //console.log('error', error)
          if (error === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消删除',
            })
          }
        })
    },
  },
}
</script>
